$(document).ready(function(){
	$("a.btnAdd").click(function(){
		var amount=getNumeric($("#amount").val());
		var add=parseFloat($(this).attr("val"));
		amount=amount+add;
		$('#amount:enabled').val(amount);
		setAmountValue();
		return false;
	});
	
	$('div.pgs input:radio').click(function(e){
		var str=$(this).attr('id');
		$('.forPg').hide();
		$('.'+str).show();
		toggleBtnSubmit();
	});
	
	$('form#credit_card_form input').click(function(e){
		toggleBtnSubmit();
	});
	
	$('form#credit_card_form input').keyup(function(e){
		toggleBtnSubmit();
	});
	
	$("#amount").keyup(function(e){
		if($(this).val()){
			setAmountValue();
		}
	});
	
	$('a#btn_submit').click(function(e){
		var card_id=$('form.credit_card_form input[name="data[id]"]:checked').val();
		if($('div.pgs input:radio:checked').val()==='credit_card'&&card_id==='new_credit_card'&&$('.billing-address:hidden').length>0){
			$('.billing-address').show();
			//$('a#btn_submit').html('Pay');
			return;
		}
		$(this).siblings('.loading').show();
		setAmountValue();
		if($('div.pgs input:radio:checked').val()==='credit_card'){
			submitCreditCardForm();
		}else if($('div.pgs input:radio:checked').val()==='alipay'){
			submitAlipayForm();
		}else if($('div.pgs input:radio:checked').val()==='deposit'){
			$("form#deposit_form").submit();
		}
		return false;
	});
});

function toggleBtnSubmit(){
	var method=$('input[name="data[pgs]"]:checked').val();
	var card_id=$('form#credit_card_form input[name="data[id]"]:checked').val();
	var number=$('form#credit_card_form input#card_number').val();
	var type=$('form#credit_card_form select#card_type').val();
	var mon=$('form#credit_card_form select#exp_month').val();
	var year=$('form#credit_card_form select#exp_year').val();
	var csc=$('form#credit_card_form input#csc').val();
	if(method==='credit_card'&&card_id==='new_credit_card'){
		if(type&&number&&mon&&year&&csc){
			$('a#btn_submit').html('Next');
			$('a#btn_submit').show();
		}else{
			$('a#btn_submit').hide();
		}
	return;
	}
	//$('a#btn_submit').html('Pay');
	$('a#btn_submit').show();
}

function setAmountValue(){
	var amount=getNumeric($("#amount").val());
	$('input[name="amount"]').val(amount);
	$('input[name="data[amount]"]').val(amount);
	return amount;
}


function submitCreditCardForm(){
	var card_id=$('form#credit_card_form input[name="data[id]"]:checked').val();
	var valid=$("form#credit_card_form").ccvalidate({
		cardType:'#card_type',
		cardNumber:'#card_number'});
	if(card_id==='new_credit_card'&&!valid){
		makeErrorMessage("Please enter a valid credit card number and type.");
		$('a#btn_submit').siblings('.loading').hide();
		return false;
	}
	$("form#credit_card_form").ajaxSubmit({
		url:$("form#credit_card_form").attr('action')+'.json',
		success:function(data){
			if(data.error){
				makeErrorMessage(data.error);
			}else{
				makeErrorMessage(data.success);
			}
			$('a#btn_submit').siblings('.loading').hide();
		}
	});
}

function submitAlipayForm(){
	var amount=getNumeric($("#amount").val());
	$.post($('form#alipay_form').attr("beforeSubmit")+amount+'.json',{"data[amount]":amount},function(data){
		if(data.error){
			makeErrorMessage(data.error);
			$('a#btn_submit').siblings('.loading').hide();
		}else{
			$("form#alipay_form").children("input[name=custom]").attr("value",data.custom);
			$("form#alipay_form").submit();
		}
	});
}